<template>
  <Modal
    v-model="showCrontabHelpInfoModal"
    title="Crontab帮助信息"
    :mask-closable="false"
    :closable="false"
    fullscreen
  >
    <MarkdownEditor
      :content="markdownInfo.content"
      @save="save"
    ></MarkdownEditor>
  </Modal>
</template>

<script>
import MarkdownEditor from "_c/markdown";
import {
  getMarkdowns,
  createMarkdown,
  updateMarkdown,
} from "@/api/HttpAutoTestService/markdown";
export default {
  name: "crontab-help-info",
  components: {
    MarkdownEditor,
  },
  props: {
    crontabHelpInfo: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    crontabHelpInfo(val) {
      if (val) {
        getMarkdowns({ types: this.markdownInfo.types }).then((res) => {
          if (res.status === 200 && res.data.count) {
            const data = res.data;
            this.markdownInfo.id = data.results[0].id;
            this.markdownInfo.content = data.results[0].content;
          }
        });
      }
    },
  },
  computed: {
    showCrontabHelpInfoModal: {
      get() {
        return this.crontabHelpInfo;
      },
      set(val) {
        this.$emit("update:crontabHelpInfo", val);
      },
    },
  },
  data() {
    return {
      markdownInfo: {
        id: "",
        name: "Crontab帮助信息",
        types: "CRONTAB_HELP_INFO",
        content: "",
      },
    };
  },
  methods: {
    // 保存content
    save(value, render) {
      this.markdownInfo.content = value;
      if (this.markdownInfo.id) {
        updateMarkdown(this.markdownInfo).then((res) => {});
      } else {
        delete this.markdownInfo.id;
        createMarkdown(this.markdownInfo).then((res) => {
          if (res.status === 201) {
            this.markdownInfo.id = res.data.id;
          }
        });
      }
    },
  },
};
</script>
  <style scoped>
</style>